package com.gensdai.leliang.retrofitUtils.conver;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

/**StringConverter的实现
 * Created by liutao on 2017/5/24.
 */

public class StringConver implements Converter<ResponseBody, String>{



    public static StringConver INS;
    public static StringConver getInstance(){
        if(INS == null){
            INS = new StringConver();
        }

        return INS;
    }

    @Override
    public String convert(ResponseBody value) throws IOException {
        return value.string();
    }

    public static class StringConverterFactory extends Converter.Factory {

        public static final StringConverterFactory INSTANCE = new StringConverterFactory();

        public static StringConverterFactory create() {
            return INSTANCE;
        }

        // 我们只关实现从ResponseBody 到 String 的转换，所以其它方法可不覆盖
        @Override
        public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
            if (type == String.class) {
                return StringConver.getInstance();
            }
            //其它类型我们不处理，返回null就行
            return null;
        }
    }
}
